

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

class Connection {

    String name;

    String reso;

    List children = new ArrayList();

}

class Table {

    String name;

    String reso;

}

class TestProvider extends LabelProvider implements ITableLabelProvider, ITreeContentProvider {

    public Image getColumnImage(Object element, int columnIndex) {

        return null;
    }

    public String getColumnText(Object element, int columnIndex) {
        switch (columnIndex) {
        case 0:
            if (element instanceof Connection) {
                Connection c = (Connection) element;
                return c.name;
            }
            if (element instanceof Table) {
                Table t = (Table) element;
                return t.name;
            }
            break;
        case 1:
            if (element instanceof Connection) {
                Connection c = (Connection) element;
                return c.reso;
            }
            if (element instanceof Table) {
                Table t = (Table) element;
                return t.reso;
            }

            break;
        default:
            break;
        }
        return "--";
    }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // TODO Auto-generated method stub

    }

    public Object[] getElements(Object inputElement) {
        List list = (List) inputElement;
        return list.toArray();
    }

    public Object[] getChildren(Object parentElement) {
        if (parentElement instanceof Connection) {
            Connection c = (Connection) parentElement;
            return c.children.toArray();
        }
        return new Object[0];
    }

    public Object getParent(Object element) {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean hasChildren(Object element) {
        return true;
    }

}

public class TestTreeTable {

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        TableTreeViewer v = new TableTreeViewer(shell);

        TreeViewer viewer = new TreeViewer(shell);
        Tree tree = viewer.getTree();
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);

        TreeColumn column1 = new TreeColumn(tree, SWT.CENTER);
        column1.setText("Database");
        column1.setWidth(200);
        TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
        column2.setText("Repository");
        column2.setWidth(200);

        TestProvider pro = new TestProvider();
        viewer.setLabelProvider(pro);
        viewer.setContentProvider(pro);

        viewer.setInput(getInput());

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private static Object getInput() {
        Connection c1 = new Connection();
        c1.name = "MySql";
        c1.reso = "my sql's repository";
        Table t1 = new Table();
        t1.name = "User";
        t1.reso = "T xxx";
        c1.children.add(t1);

        Connection c2 = new Connection();
        c2.name = "MySql";
        c2.reso = "my sql's repository";
        Table t2 = new Table();
        t2.name = "User xxcv";
        t2.reso = "T xxx    sdfsf";
        c2.children.add(t2);
        Table t3 = new Table();
        t3.name = "User";
        t3.reso = "T xxx";
        c2.children.add(t3);

        List list = new ArrayList();
        list.add(c1);
        list.add(c2);
        return list;
    }
}
